package com.walker.kuyun.ui.adapter

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.walker.kuyun.App
import com.walker.kuyun.R
import com.walker.kuyun.bean.VideoCoverBean
import com.walker.kuyun.config.Const
import com.walker.kuyun.util.Cover3ViewHolder
import com.walker.kuyun.util.glide.GlideUtil.loadImage
import com.walker.kuyun.util.Util.process
import com.walker.kuyun.util.Util.showToast
import com.walker.kuyun.util.ViewHolderUtil.Companion.getItemViewType
import com.walker.kuyun.util.ViewHolderUtil.Companion.getViewHolder
import com.walker.kuyun.util.gone
import com.walker.kuyun.util.visible

class SearchTMAdapter(
    val activity: Activity,
    private val dataList: List<VideoCoverBean>
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun getItemViewType(position: Int): Int = getItemViewType(dataList[position])

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder =
        getViewHolder(parent, viewType)

    override fun getItemCount(): Int = dataList.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = dataList[position]

        when (holder) {
            is Cover3ViewHolder -> {
                holder.ivAnimeCover3Cover.setTag(R.id.image_view_tag, item.cover?.url)
                if (holder.ivAnimeCover3Cover.getTag(R.id.image_view_tag) == item.cover?.url) {
                    holder.ivAnimeCover3Cover.loadImage(
                        activity,
                        item.cover?.url ?: "",
                        referer = item.cover?.referer
                    )
                }
                holder.tvAnimeCover3Title.text = item.title
                if (item.episode == "") {
                    holder.tvAnimeCover3Episode.gone()
                } else {
                    holder.tvAnimeCover3Episode.visible()
                    holder.tvAnimeCover3Episode.text = item.episode
                }
                item.videoType?.let {
                    holder.flAnimeCover3Type.removeAllViews()
                    for (i in it.indices) {
                        val tvFlowLayout: TextView = activity.layoutInflater
                            .inflate(
                                R.layout.item_tm_type_1,
                                holder.flAnimeCover3Type,
                                false
                            ) as TextView
                        tvFlowLayout.text = it[i].title
                        tvFlowLayout.setOnClickListener { _ ->
                            //此处是”类型“，若要修改，需要注意Tab大分类是否还是”类型“
                            process(
                                activity,
                                "${Const.ActionUrl.ITEM_CLASSIFY}${it[i].actionUrl}类型/${it[i].title}"
                            )
                        }
                        holder.flAnimeCover3Type.addView(tvFlowLayout)
                    }
                }
                holder.tvAnimeCover3Describe.text = item.describe
                holder.itemView.setOnClickListener {
                    process(activity, item.actionUrl)
                }
            }
            else -> {
                holder.itemView.visibility = View.GONE
                (App.getApp().resources.getString(R.string.unknown_view_holder) + position).showToast()
            }
        }
    }
}